<script setup>
import { reactive, ref, onMounted, watch } from 'vue';
import { positionApi } from '@T2/service/api.ts';
import { buildTree } from '@T2/utils/utils.ts';

const props = defineProps({
  units: {
    type: Array,
    requied: true,
  }
});

const Rt = reactive({
  units: [],
})
const Filter = reactive({
  dabiao: null,
  positionsub: null,
  warn: false,
  offline: false,
  
  
});
watch(() => ({...Filter}), (nv, ov) => {
  let arr = props.units;
  if (nv.dabiao === true) {
    arr = arr.filter(i => !i.warn_number);
  } else if (nv.dabiao === false) {
    arr = arr.filter(i => i.warn_number);
  }
  if (nv.warn) {
    arr = arr.filter(i => i.warn_number);
  }
  if (nv.offline) {
    arr = arr.filter(i => i.offline);
  }
  Rt.units = arr;
});

onMounted(() => {
  Rt.units = props.units;
  getPosition();
});

const options = ref([]);
const getPosition = () => {
  positionApi({
    method: 'get',
    params: {
      ordering: 'id',
    }
  }).then(res => {
    options.value = buildTree(res?.results || [], 'parent');
  });
}
const handleUnitClick = (unit) => {
  props.units.forEach(i => i._isActive = i.id === unit.id);
}
</script>

<template>
  <div class="container-bg flex-column">
    <div class="header-tab flex">
      <!-- <el-cascader
        class="positionC"
        popper-class="positionC-popper"
        v-model="Filter.positionsub"
        :options="options"
        size="small"
        :teleported="false"
        :clearable="true"
        placeholder="全部"
        :props="{value: 'id', label: 'name' }"
      /> -->
      <!-- <el-select
        size="small"
        class="dabiao"
        v-model="Filter.dabiao"
        placeholder="全部"
        :teleported="false"
        clearable
        @clear="Filter.dabiao = null"
      >
        <el-option label="达标" :value="true">达标</el-option>
        <el-option label="不达标" :value="false">不达标</el-option>
      </el-select> -->
      <button :class="{ 'isActive': Filter.dabiao }" @click="Filter.dabiao = !Filter.dabiao">全部</button>
      <button :class="{ 'isActive': Filter.dabiao }" @click="Filter.dabiao = !Filter.dabiao">达标</button>
      <button :class="{ 'isActive': Filter.warn }" @click="Filter.warn = !Filter.warn">报警</button>
      <button :class="{ 'isActive': Filter.offline }" @click="Filter.offline = !Filter.offline">离线</button>
    </div>
    <div class="list-box scroller-header">
      <template v-for="item in Rt.units">
        <div class="unit-item flex--center" :class="{ 'isActive': item._isActive }" @click="handleUnitClick(item)">
          <img :src="item.warn_number ? require('@T2/assets/maps/toilet2-warn.png') : require('@T2/assets/maps/toilet2.png')" />
          <p>{{ item.name }}</p>
          <el-icon color="rgba(255,255,255,0.65)">
            <ArrowRight v-if="!item._isActive"/>
            <ArrowDown v-else />
          </el-icon>
        </div>
        <div class="grid-unit" v-if="item._isActive && item._sensors?.length">
          <div class="sen-item flex--center" v-for="sen in (item._sensors[0]?.c || [])">
            {{ sen.n }}：
            <strong :class="{ 'warn': !sen.state }">{{ sen.value }}</strong>
            &nbsp;{{ sen.unit }}
          </div>
          <div class="sen-item flex--center" v-if="item._sensors[0]?.c?.length">
            上传时间：{{ item._sensors[0]?.c[0]?.timestamp }}
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container-bg {
  width: 404px;
  height: 619px;
  background: url('@T2/assets/bg/bg01.png') no-repeat center;
  background-size: 100% 100%;
  padding: 33px 16px 20px;
}
.header-tab {
  height: 26px;
  >button {
    background-color: transparent;
    border: none;
    padding: 0;
    text-align: center;
    width: 60px;
    height: 26px;
    line-height: 26px;
    font-weight: 500;
    font-size: 14px;
    color: #14777E;
    cursor: pointer;
    &.isActive {
      color: #00FFFF;
    }
  }
  .dabiao {
    width: 60px;
    height: 26px;
  }
}
.list-box {
  flex: 1;
  overflow: auto;
  margin-top: 15px;
  .unit-item {
    height: 44px;
    border-radius: 4px;
    padding: 0 8px;
    font-weight: 400;
    font-size: 14px;
    color: #FFFFFF;
    cursor: pointer;
    &.isActive,
    &:hover {
      background-color: rgba(0,69,69,0.4);
    }
    img {
      width: 24px;
      height: 24px;
    }
    p {
      margin-left: 15px;
    }
    .el-icon {
      margin-left: auto;
    }
  }
  .grid-unit {
    display: grid;
    gap: 2px;
    grid-template-columns: 1fr 1fr;
    margin-top: 2px;
    margin-bottom: 8px;
    .sen-item {
      height: 30px;
      background: rgba(21,56,67,0.5);
      font-weight: 400;
      font-size: 12px;
      color: #D9D9D9;
      padding-left: 8px;
      strong {
        font-weight: 600;
        font-size: 14px;
        color: #00FFFF;
        &.warn {
          color: #FF0000;
        }
      }
    }
  }
}
</style>

<style scoped>
:deep(.dabiao .el-select__wrapper) {
  padding: 0;
  background-color: transparent;
  box-shadow: none;
}
:deep(.dabiao .el-select__selected-item) {
  color: #00FFFF;
  text-align: center;
}
:deep(.dabiao .el-select__placeholder.is-transparent) {
  color: #14777E;
}
:deep(.dabiao .el-select__suffix .el-select__caret) {
  color: #14777E;
}

:deep(.dabiao .el-popper.is-light) {
  background-color: #002F3A;
  border: none;
  box-shadow: 0px 2px 8px 0px rgba(0,0,0,0.15);
}
:deep(.dabiao .el-popper__arrow::before) {
  background-color: #002F3A;
  border-color: #002F3A;
}
:deep(.dabiao .el-select-dropdown__item) {
  color: #DEDEDE;
}
:deep(.dabiao .el-select-dropdown__item.is-hovering) {
  background-color: #054553;
  color: #00FFFF;
}

:deep(.positionC .el-input) {
  width: fit-content;
  min-width: 60px;
  max-width: 160px;
}
:deep(.positionC .el-input__wrapper) {
  background-color: transparent;
  box-shadow: none;
}
:deep(.positionC .el-input__wrapper:hover) {
  box-shadow: none;
}
:deep(.positionC .el-input.is-focus .el-input__wrapper) {
  box-shadow: none !important;
}
:deep(.positionC .el-input__inner), 
:deep(.positionC .el-input__inner::placeholder) {
  color: #14777E;
}
:deep(.positionC .el-input__suffix) {
  color: #14777E;
}

:deep(.positionC-popper.el-popper.is-pure) {
  background-color: #002F3A;
  box-shadow: 0px 2px 8px 0px rgba(0,0,0,0.15);
  border-radius: 2px;
  border: none;
}
:deep(.positionC-popper .el-cascader-menu) {
  border-right: 1px solid rgba(0,0,0,0.15);
}
:deep(.positionC-popper .el-cascader-node) {
  color: #DEDEDE;
}
:deep(.positionC-popper .el-cascader-node:hover),
:deep(.positionC-popper .el-cascader-node:focus) {
  background-color: #054553 !important;
}
:deep(.positionC-popper .el-cascader-node.in-active-path),
:deep(.positionC-popper .el-cascader-node.is-active){
  color: #00FFFF;
  background-color: #054553;
}
:deep(.positionC-popper .el-popper__arrow::before) {
  background-color: #002F3A;
  border-color: #002F3A;
}
</style>